$(function () {
    $('#btn_upload').click(function () {

        if($('#uploadForm').form('validate')){
            showProgressbar();
            $("#uploadForm").form('submit', {
                url: '/upload',
                data: $("#uploadForm").serialize(),
                success: function (result) {
                    result=$.parseJSON(result);
                    if (result.success) {
                        $('#progressbar').progressbar('setValue', 100);
                        $("#uploadForm").window('close');
                        $.messager.alert('消息', result.message);
                    } else {
                        $('#progressbar').progressbar('setValue', 100);
                        $.messager.alert('消息', result.message);
                    }

                }
            });
        }

    });
});

function showProgressbar() {
    var pb = $('#progressbar');
    pb.progressbar({value: 1});
    setInterval(function () {
        var value = pb.progressbar('getValue');
        if (value < 100) {
            value += Math.floor(Math.random() * 10);
            pb.progressbar('setValue', value);
        }
    }, 1000)
}

function showUploadPanel() {
    $('#uploadForm').window({
        modal: true
    });
}

function showQueryPanel(type) {
    switch (type) {
        case 'student':
            $('#studentQueryPanel').window({
                modal: true
            });
            break;
        case
        'teacher':
            $('#teacherQueryPanel').window({
                modal: true
            });
            break;
        default:
            break;
    }

}

function pageQuery(newQuery, url, params, dg, queryWindow) {

    var pager = dg.datagrid('getPager');

    if (newQuery) {
        pager.pagination({pageNumber: 1});
    }
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            pageQuery(false, url, params, dg);
        }
    });
    var pageNumber = pager.pagination('options').pageNumber;
    var pageSize = pager.pagination('options').pageSize;
    pageNumber = pageNumber > 0 ? pageNumber - 1 : pageNumber;
    params.pageSize = pageSize;
    params.pageNumber = pageNumber;
    $.post(url, params, function (result) {
        if (result.success) {
            dg.datagrid('loadData', result.data);
            if (newQuery) {
                queryWindow.window('close');
            }
        } else {
            $.messager.alert('消息', result.message);
        }


    });
}

function queryByStrudent(newQuery) {

    var dg = $('#dg');
    var queryWindow = $('#studentQueryPanel');
    var url = '/queryByStudent';
    var params = {
        studentCode: $('#sqp_sc').val(),
        studentName: $('#sqp_sn').val()
    };
    if (queryWindow.form('validate')) {
        pageQuery(newQuery, url, params, dg, queryWindow);
    }

}

function teacherQuery(newQuery) {
    var dg = $('#dg');
    var queryWindow = $('#teacherQueryPanel');
    var url = '/teacherQuery';
    var params = {
        username: $('#tqp_username').val(),
        password: $('#tqp_password').val(),
        classCode: $('#tqp_c').val(),
        className: $('#tqp_c').val(),
        studentCode: $('#tqp_sc').val(),
        studentName: $('#tqp_sn').val(),
        qf: $('#tqp_cb')[0].checked
    };
    if (queryWindow.form('validate')) {
        pageQuery(newQuery, url, params, dg, queryWindow);
    }
}

var btsloader = function (param, success, error) {

    //获取输入的值
    var q = param.q || "";

    //此处q的length代表输入多少个字符后开始查询
    if (q.length <= 0) return false;
    $.ajax({
        url: "/getClasses",
        type: "post",
        data: {
            //传值，还是JSON数据
            helpcode: q
        },

        //重要，如果写jsonp会报转换错误，此处不写都可以
        dataType: "json",
        success: function (data) {

            //关键步骤，遍历一个MAP对象
            var items = $.map(data, function (item) {
                return {
                    id: item.code,
                    name: item.name
                };
            });

            //执行loader的success方法
            success(items);
        },

        //异常处理
        error: function (xml, text, msg) {
            error.apply(this, arguments);
        }
    });
};

